﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using System.Windows.Controls;

namespace Pavilion.DesignerTool.ViewTools
{
    public class PropertyItemWrapper
    {
        public string PropertyName { get { return PropertyItem.PropertyName; } }
        public object PropertyValue { get { return PropertyItem.PropertyValue; } set { PropertyItem.PropertyValue = value; LoadPropertyValue(); } }
        public IPropertyItem PropertyItem { get; set; }
        public FrameworkElement FrameworkElement { get; set; }
        public double Width { get; set; }

        public PropertyItemWrapper(IPropertyItem propertyItem)
        {
            this.PropertyItem = propertyItem;

            LoadPropertyValue();
        }

        private void LoadPropertyValue()
        {
            if (PropertyValue is bool)
                LoadCheckBox();
            else if (PropertyValue is Enum)
                LoadComboBox();
            else if (PropertyValue is int || PropertyValue is float || PropertyValue is double)
                LoadNumericTextBox();
            else
                LoadTextBox();
        }

        private void LoadTextBox()
        {
            if (FrameworkElement is TextBox)
                return;

            TextBox textBox = new TextBox();
            FrameworkElement = textBox;

            if (PropertyValue != null)
                textBox.Text = PropertyValue.ToString();

            textBox.SetBinding(TextBox.TextProperty, CreateBinding());
        }

        private void LoadNumericTextBox()
        {
            if (FrameworkElement is TextBox)
                return;

            TextBox numericTextBox = new TextBox();
            FrameworkElement = numericTextBox;

            numericTextBox.Text = PropertyValue.ToString();
            numericTextBox.LostFocus += new RoutedEventHandler(NumericTextBox_LostFocus);
        }

        private void LoadComboBox()
        {
            if (FrameworkElement is ComboBox)
                return;

            ComboBox comboBox = new ComboBox();
            FrameworkElement = comboBox;

            comboBox.Style = (Style) comboBox.FindResource(ToolBar.ComboBoxStyleKey);
            comboBox.ItemsSource = Enum.GetValues(PropertyValue.GetType());
            comboBox.SelectedItem = PropertyValue;
            comboBox.HorizontalAlignment = HorizontalAlignment.Stretch;
            comboBox.SetBinding(ComboBox.SelectedItemProperty, CreateBinding());
        }

        private void LoadCheckBox()
        {
            if (FrameworkElement is CheckBox)
                return;

            CheckBox checkBox = new CheckBox();
            FrameworkElement = checkBox;

            checkBox.IsChecked = (bool)PropertyValue;
            checkBox.VerticalAlignment = VerticalAlignment.Center;
            checkBox.SetBinding(CheckBox.IsCheckedProperty, CreateBinding());
        }

        private Binding CreateBinding()
        {
            Binding binding = new Binding();
            binding.Source = this;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            binding.Path = new PropertyPath("PropertyValue");

            return binding;
        }

        private void NumericTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;

            try
            {
                if (PropertyValue is int)
                    PropertyValue = int.Parse(textBox.Text);
                else if (PropertyValue is float)
                    PropertyValue = float.Parse(textBox.Text);
                else if (PropertyValue is double)
                    PropertyValue = double.Parse(textBox.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                textBox.Text = PropertyValue.ToString();
            }
        }
    }
}
